home *** CD-ROM | disk | FTP | other *** search
/ Apple Developer Connection 1998 Fall: Game Toolkit / Disc.iso / Samples / Moofwars 1.02 / MoofWars Sprocket / •Source / TSprite.cp < prev    next >
Encoding:
Text File  |  1997-11-05  |  4.9 KB  |  203 lines  |  [TEXT/CWIE]

  1. /*************************************************************************************
  2. TSprite.cp
  3.  
  4. This implements a first cut at a standardized sprite class.  This is an abstract base
  5. class and all sprites must be subclassed off a TSprite.
  6.  
  7. Author: Timothy Carroll
  8. Apple Developer Technical Support
  9. timc@apple.com
  10.  
  11. Modification History: 
  12. 1/23/97     TMC        Added include for Moofwars.h so that MrC will compile
  13. 8/15/96        TMC     Initial Release
  14.  
  15. Copyright © 1996, 1997 Apple Computer, Inc., All Rights Reserved
  16.  
  17. You may incorporate this sample code into your applications without
  18. restriction, though the sample code has been provided "AS IS" and the
  19. responsibility for its operation is 100% yours.  However, what you are
  20. not permitted to do is to redistribute the source as "DSC Sample Code"
  21. after having made changes. If you're going to re-distribute the source,
  22. we require that you make it clear in the source that the code was
  23. descended from Apple Sample Code, but that you've made changes.
  24. *************************************************************************************/
  25.  
  26. #include "Moofwars.h"
  27. #include "TSprite.h"
  28. #include "TSpriteCollection.h"
  29. #include "DrawSprocket.h"
  30.  
  31.  
  32. /*************************************************************************************
  33.     Constructors and Destructors    
  34. *************************************************************************************/
  35.  
  36.  
  37. TSprite::TSprite (TSpriteData *data)
  38. {
  39.     fCoordX  = data->initialX;
  40.     fCoordY  = data->initialY;
  41.     
  42.     fVelocityX = data->initialXVelocity;
  43.     fVelocityY = data->initialYVelocity;
  44.     
  45.     fFace = data->face;
  46.     fVisibility = data->visibility;
  47.     
  48.     fGroup = NULL;
  49.     fNextSprite = fPrevSprite = NULL;
  50.     
  51.     if (data->preloadedCollection != NULL)
  52.     {
  53.         fSpriteImages = data->preloadedCollection;
  54.         fSpriteImages->AddReference();
  55.     }
  56.     else
  57.         fSpriteImages = TGraphicCollection::NewCollection (data->collectionID);
  58.     
  59.     fBounds = fSpriteImages->GetBounds(0);
  60.     
  61.     fXOffset = (fBounds.right - fBounds.left) >> 1;
  62.     fYOffset = (fBounds.bottom - fBounds.top) >> 1;
  63. }
  64.  
  65. TSprite::~TSprite (void)
  66. {
  67.     if (fSpriteImages)
  68.         fSpriteImages->DisposeReference();
  69.     fSpriteImages = NULL;
  70.     RemoveFromGroup();
  71. }
  72.  
  73. /*************************************************************************************
  74.     Standard Accessor Functions
  75.     
  76. *************************************************************************************/
  77.     
  78. void            
  79. TSprite::GetCurrentWorldLocation  (SInt32 *worldX, SInt32 *worldY)
  80. {
  81.     *worldX = fCoordX;
  82.     *worldY = fCoordY;
  83. }
  84.     
  85. void
  86. TSprite::SetCurrentWorldLocation (SInt32 worldX, SInt32 worldY)
  87. {
  88.     fCoordX = worldX;
  89.     fCoordY = worldY;
  90. }
  91.  
  92. SInt32
  93. TSprite::GetXVelocity()
  94. {
  95.     return fVelocityX;
  96. }
  97.  
  98. SInt32
  99. TSprite::GetYVelocity()
  100. {
  101.     return fVelocityY;
  102. }
  103.  
  104. void
  105. TSprite::SetXVelocity (SInt32 xVelocity)
  106. {
  107.     fVelocityX = xVelocity;
  108. }
  109.  
  110. void
  111. TSprite::SetYVelocity (SInt32 yVelocity)
  112. {
  113.     fVelocityY = yVelocity;
  114. }
  115.  
  116.  
  117. void
  118. TSprite::DrawSprite (void)
  119. {
  120.     // calculate the top left corner of the current world coordinates given our scaling
  121.     // information.  We pass this along to the TGraphic for the current scale.  It is considered
  122.     // an error to draw in a scale that has no collection of graphics.
  123.     
  124.     if (fVisibility == kVisible)
  125.     {
  126.         // convert to QD coordinates
  127.         SInt32                 spriteH, spriteV;
  128.  
  129. #if qDebugging
  130.         if (fSpriteImages == NULL)
  131.         {
  132.             DebugStr ("\pAttempted to draw with an invalid scale");
  133.             return;
  134.         }
  135. #endif
  136.  
  137. // The ability to draw the sprite in different sizes is nice, but it might cause too much
  138. // overhead.  Need to test this.
  139.         
  140.         spriteH = ((fCoordX - gWorldCoordX) >> 16) + gClipCenterX - fXOffset;
  141.         spriteV = ((fCoordY - gWorldCoordY) >> 16) + gClipCenterY - fYOffset;
  142.  
  143.         fSpriteImages->CopyImage (fFace, spriteV, spriteH, kDrawGraphic);
  144.     }
  145. }
  146.  
  147.  
  148. void
  149. TSprite::Bounce (WorldRect32 *bounceBounds)
  150. {
  151.     SInt32 xOff = fXOffset << 16;
  152.     SInt32 yOff = fYOffset << 16;
  153.     
  154.     if (((fCoordX-xOff < bounceBounds->left) && (fVelocityX < 0)) ||
  155.         ((fCoordX+xOff > bounceBounds->right) && (fVelocityX > 0)))
  156.     {
  157.         fVelocityX = -fVelocityX;
  158.         fCoordX += fVelocityX;
  159.     }
  160.  
  161.     if (((fCoordY-yOff < bounceBounds->top) && (fVelocityY < 0)) ||
  162.         ((fCoordY+yOff > bounceBounds->bottom) && (fVelocityY > 0)))
  163.     {
  164.         fVelocityY = -fVelocityY;
  165.         fCoordY += fVelocityY;
  166.     }
  167.  
  168. }
  169.  
  170.  
  171. void
  172. TSprite::Collision (TSprite *theSprite)
  173. {
  174. #pragma unused (theSprite)
  175.     // by default we do nothing in a collision
  176. }
  177.  
  178.  
  179. /*************************************************************************************
  180.     Routines that link up with the TSpriteCollection class.  Note that these routines should
  181.     always be called.  Don't call the routines in the TSpriteCollection class, or things won't
  182.     be updated properly.
  183.     
  184. *************************************************************************************/
  185.         
  186. void
  187. TSprite::AddToGroup (TSpriteCollection *theGroup)
  188. {
  189.     // clear any existing group
  190.     RemoveFromGroup();
  191.     theGroup->AddSprite (this);
  192.     fGroup = theGroup;
  193. }
  194.  
  195. void
  196. TSprite::RemoveFromGroup (void)
  197. {
  198.     if (fGroup != NULL)
  199.     {
  200.         fGroup->RemoveSprite(this);
  201.         fGroup = NULL;
  202.     }
  203. }